package com.jsyl.lwbk.framework.shiro.service;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.jsyl.lwbk.common.utils.DateUtils;
import com.jsyl.lwbk.framework.manager.factory.AsyncFactory;
import com.jsyl.lwbk.system.domain.SysCenter;
import com.jsyl.lwbk.system.domain.UserCenter;
import com.jsyl.lwbk.system.mapper.SysCenterMapper;
import com.jsyl.lwbk.system.mapper.UserCenterMapper;
import com.jsyl.lwbk.system.utils.encrypt.CodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import com.jsyl.lwbk.common.constant.Constants;
import com.jsyl.lwbk.common.constant.ShiroConstants;
import com.jsyl.lwbk.common.constant.UserConstants;
import com.jsyl.lwbk.common.utils.MessageUtils;
import com.jsyl.lwbk.common.utils.ServletUtils;
import com.jsyl.lwbk.framework.manager.AsyncManager;
import com.jsyl.lwbk.system.domain.SysUser;
import com.jsyl.lwbk.system.service.ISysUserService;

/**
 * 注册校验方法
 *
 *
 */
@Component
public class SysRegisterService {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private SysPasswordService passwordService;
    @Autowired
    private UserCenterMapper userCenterMapper;
    @Autowired
    private SysCenterMapper sysCenterMapper;

    /**
     * 注册
     */
    public String register(SysUser user) {
        String msg = "", loginName = user.getLoginName(), password = user.getPassword();

        if (!StringUtils.isEmpty(ServletUtils.getRequest().getAttribute(ShiroConstants.CURRENT_CAPTCHA))) {
            msg = "验证码错误";
        } else if (StringUtils.isEmpty(loginName)) {
            msg = "用户名不能为空";
        } else if (StringUtils.isEmpty(password)) {
            msg = "用户密码不能为空";
        } else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
            msg = "密码长度必须在5到20个字符之间";
        } else if (loginName.length() < UserConstants.USERNAME_MIN_LENGTH
                || loginName.length() > UserConstants.USERNAME_MAX_LENGTH) {
            msg = "账户长度必须在2到20个字符之间";
        } else if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(loginName))) {
            msg = "保存用户'" + loginName + "'失败，注册账号已存在";
        } else {
            user.setUserName(loginName);
            user.setPwd(password);
            String pwd = CodeUtil.getInstance().encodeMD5(password, user.getLoginName().toLowerCase());
            user.setPassword(pwd);
            //新注册用户设置为待审核状态
            user.setStatus(1);
            boolean regFlag = userService.registerUser(user);
            if (!regFlag) {
                msg = "注册失败,请联系系统管理人员";
            } else {
                if(!com.jsyl.lwbk.common.utils.StringUtils.isEmpty(user.getCenterName())){
                    SysCenter center =new SysCenter();
                    center.setId(IdWorker.getId());
                    center.setCenterName(user.getCenterName());
                    center.setStatus(0);
                    center.setCreateTime(DateUtils.getNowDate());
                    this.sysCenterMapper.insertCenter(center);
                    SysCenter sysCenter= this.sysCenterMapper.getById(center.getId());
                    UserCenter userCenter =new UserCenter();
                    userCenter.setId(IdWorker.getId());
                    userCenter.setUserId(user.getUserId());
                    userCenter.setCenterId(sysCenter.getCenterId());
                    userCenter.setStatus(0);
                    userCenter.setIsAdmin(1);
                    userCenterMapper.insertUserCenter(userCenter);
                }
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.REGISTER, MessageUtils.message("user.register.success")));
            }
        }
        return msg;
    }
}
